<template>
  <div>
    <!-- 主体区域 -->
    <main>
      <div class="w1200">
        <div class="gap"></div>
        <div class="wfd">
          <div class="h160">
            <span><img src="@/assets/imgs/HIW_slices/HIW.png" alt="" /></span>
            <span class="pdl15">
              <span class="title">{{ eSubjectType.title }}</span>
              <span class="des">{{ eSubjectType.tips }}</span>
            </span>
          </div>
          <div class="h60">
            <span class="exnum">{{ iNum }}.{{ subject.title }}</span>
            <span class="labels">
              <span v-if="subject.isJt == '1'">机经</span>
              <span v-if="subject.isJt !== '1'" style="background: #7cc3d6"
                >练习题</span
              >
              <span
                style="background: #ff5e4e"
                v-if="subject.labelStyle == 'wrong-color'"
                >错题</span
              >
              <span style="background: #13bfbf" v-if="subject.isYc == '1'"
                >本周预测</span
              >
              <span style="background: #99bbe4" v-if="practiceList.length>0"
                >练习{{ practiceList.length }}次</span
              >
              <span
                style="background: #94d0f7"
                v-if="practiceList.length > 0"
                >{{ subject.titleNumber }}</span
              >
              <span style="background: #94d0f7">{{ subject.titleNumber }}</span>
            </span>
            <Label
              v-if="subject.labelStyle != undefined"
              :df="subject.labelStyle"
              :id="subject.id"
            ></Label>
          </div>
          <!-- 录音播放区域 -->
          <div class="h121">
            <div>
              <AudioPlayer
                v-if="subject.url != null"
                :url="subject.url"
                :readyTime="readyTime"
                v-on:func="checkPlay"
              ></AudioPlayer>
            </div>
          </div>
        </div>
        <div class="timu">
          <!-- 题目组件 -->
          <Incorrect
            ref="incorrect"
            v-for="(item, index) in content"
            :key="index"
            :content="item"
            :isBgc="isBgc"
            @sendNum="getNum($event)"
          ></Incorrect>
          <div class="time">
            <span v-if="isDt">答题时间：{{ s_to_hs(answerTime) }}</span>
            <span v-else>准备时间：{{ s_to_hs(readyTime) }}</span>
          </div>
        </div>
        <div class="gap"></div>
        <!-- 搜索，上下题目切换 -->
        <div class="h100">
          <div class="search-box">
            <el-button @click="$router.go(0)">重做</el-button>
            <span
              ref="answer"
              @click="seeAnswer"
              :class="isAnswerShow ? 'show_answer' : ''"
              class="seeAnswer"
              >查看答案</span
            >
            <el-input placeholder="输入内容/题目" v-model="keyword">
              <template slot="append">
                <div @click="searchSubject('listeningHIW')">搜索</div>
              </template>
            </el-input>
            <el-button class="prev" @click="goTo(last, '1', 'listeningHIW',subject.id)"
              >上一题</el-button
            >
            <el-button class="next" @click="goTo(next, '2', 'listeningHIW',subject.id)"
              >下一题</el-button
            >
            <el-button :disabled="doFlag == true" @click="addPractice()"
              >提交</el-button
            >
          </div>
          <div class="answer-box" v-show="isAnswerShow">
            <div class="title">参考答案</div>
            <div class="content">{{ subject.answer }}</div>
            <div class="origin">
              <div class="origin-title">原文</div>
              <div>
                {{ subject.originalText }}
              </div>
            </div>
          </div>
        </div>
        <div class="gap"></div>
        <div class="myexercise">
          <div class="bt">我的练习</div>
          <!-- 没有练习的情况下，显示暂无数据 -->
          <div v-show="1" class="no-data" v-if="practiceList.length == 0">
            <i class="iconfont icon-wushuju"></i>
            <div style="fontsize: 1rem">暂无数据</div>
          </div>

          <ul v-else>
            <li v-for="(item, index) in practiceList" :key="index">
              <span class="avatar">
                <img v-lazy="item.image" alt="" />
              </span>
              <span class="content">
                <span>
                  {{ item.nickname }}
                  <span class="time">{{ item.createDate | formatTM }}</span>
                </span>

                <span class="answer-history">{{ item.answer }}</span>
              </span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 做题中选题抽屉 -->
      <Drawer :img="imgUrl"></Drawer>
    </main>
  </div>
</template>

<script>
import Incorrect from '../../incorrect'
import Label from '../../label'
import Drawer from '../../drawer'
import listeningUtil from "@/utils/listeningUtil";
import AudioPlayer from "../../audioplayer/index";
import { sendPost } from "@/utils/http";
import api from "@/config/api";
export default {
  mixins: [listeningUtil],
  components: {
    Drawer,
    Label,
    AudioPlayer,
    Incorrect,
  },
  data () {
    return {
      value: false, // 查看答案按钮
      num: 0,
      imgUrl: '',
      countNum: 0,
      subject: {
      },
      content: '',
      doFlag: true, // 默认禁用提交答案按钮
      isBgc: false,
      isAnswerShow: false,
      iNum: '' // 题目索引号
    }
  },
  created () {
    setInterval(this.countDownReady, 1000)
    var query = this.$route.query;
    this.getData(query.id);
    this.iNum = this.$route.query.idx;
  },
  mounted () {
    sessionStorage.setItem('activeIndex', 'loged')
  },
  // 监听选中答案数量
  watch: {
    num: {
      handler () {
        // 答案长度大于0的时候，开启提交按钮
        if (this.num > 0) {
          this.doFlag = false
        } else {
          // 否则，禁用提交按钮
          this.doFlag = true
        }
      }
    },
    // 监听查看答案按钮状态
    value: {
      handler () {
        if (this.value == true) {
          this.addPractice()
        } else {
          // 关闭查看答案按钮后，把答案隐藏
          //获取所有选项
          let op = this.$refs.incorrect;
          for (var i = 0; i < op.length; i++) {
            op[i].isContentShow = false;
            op[i].isShow = false
          }

        }
      }
    }
  },
  methods: {
   //跳转题目页面
    goTo (id, type, name, nowId) {
      console.log("搜索中", type, id, name, nowId)
      // 没有搜索到题
      if (id == '0') {
        if (type == '1') {
          this.$msg({
            message: '没有上一题了',
            type: 'warning'
          });
        }
        if (type == '2') {
          this.$msg({
            message: '没有下一题了',
            type: 'warning'
          });
        }
        return
        // 搜到题
      }
      else {
        if (nowId > id) {
          // console.log(nowId,id);
          this.$router.push('/listeningHIW?id=' + id + '&idx=' + (this.iNum / 1 - 1));

        } else if (nowId < id) {
          console.log(nowId, id);
          this.$router.push('/listeningHIW?id=' + id + '&idx=' + (this.iNum / 1 + 1));
        }
      }
    },
    seeAnswer () {
      this.isAnswerShow = !this.isAnswerShow
      if (this.isAnswerShow) {
        this.$refs.answer.innerText = '隐藏答案'
      } else {
        this.$refs.answer.innerText = '查看答案'
      }
    },
    getNum (e) {
      this.num += e
      console.log(this.num);
    },
    // 重做
    reDo () {
      if (this.num > 0) {
        this.doFlag = false
      }
      // 获取所有选项
      let op = this.$refs.incorrect;
      for (var i = 0; i < op.length; i++) {
        op[i].isContentShow = false;
        op[i].isShow = false
        op[i].isWorng = false;
      }
    },
    changeState (e) {
      // console.log(e);
      // 查看了答案：禁用提交按钮，显示答案
      this.doFlag = true

    },
    getData (id) {
      var that = this
      sendPost(api.getSubjectDetails, {
        id: id,
      }).then((res) => {
        // console.log(res)
        that.subject = res.data
        that.content = res.data.content.split(' ')
        that.eSubjectType = res.data.eSubjectType

        that.countDown = res.data.readyTime
        that.recordingCountDown = res.data.answerTime
        if (res.data.practiceList) {
          that.practiceList = res.data.practiceList
        }
        that.last = res.data.last
        that.next = res.data.next
        if (that.surplusTime > 0) {
          setInterval(that.countDownTime, 1000)
        }
      })
    },

    //提交作业
    addPractice () {

      if (this.flag) {
        this.$msg.error("你已经作答，无需重复操作！")
        return
      }

      var that = this;
      let a = '';
      var that = this;
      //获取所有选项
      let op = this.$refs.incorrect;
      let answer = this.subject.answer.split(",")
      for (var i = 0; i < op.length; i++) {
        var content = op[i].content.replace(',', '').replace('.', '')
        var total = 0
        for (var j = 0; j < answer.length; j++) {

          if (content == answer[j].trim()) {
            console.log(content)
            total++
            break;
          }
        }

        if (total > 0) {
          if (op[i].isSelect == true) {
            op[i].isRight = true;

            if (a == '') {
              a = content
            } else {
              a += "," + content
            }
          } else {
            op[i].isShow = true
            // 提交答案后，显示正确答案
            op[i].isContentShow = true;

            op[i].answer = content
          }
        } else {
          if (op[i].isSelect == true) {
            op[i].isWorng = true;
            if (a == '') {
              a = content
            } else {
              a += "," + content
            }
          }
        }
      }
      that.flag = true
      sendPost(api.submitAnswer, {
        id: that.$route.query.id,
        answer: a,
        times: that.answerTime
      }).then((r) => {
        that.$msg({
          message: '答案提交成功',
          type: 'success'
        });

        this.refresh(that.$route.query.id);
        // 设置提交后答案显示的背景色和边框
      })
    },
    // 输入字数
    descInput () {
      if (this.answer.length > 2000) {
        this.answer = this.answer.slice(0, 200)
        this.$msg.error("字数不能超过2000")
      }
      this.countNum = this.answer.length
    },
  }
}
</script>

<style lang="less" scoped>
.seeAnswer {
  display: inline-block;
  background: #fffafa;
  border: 0.0625rem solid #ff5e4e;
  color: #ff5e4e;
  height: 2.5rem;
  font-size: 1.125rem;
  line-height: 2.5rem;
  border-radius: 0.3125rem;
  padding: 0 0.2875rem;
  // margin-right: 14.375rem;
  // margin-left: 7.25rem;
  cursor: pointer;
}
.show_answer {
  color: #fff;
  background-color: #ff5e4e;
}
.txt_content {
  width: 66.25rem;
  word-wrap: break-all;
  word-break: normal;
  overflow: hidden;
  flex-wrap: wrap;
  /deep/span {
    height: 2.5rem;
    line-height: 2.5rem;
    user-select: none;
    cursor: pointer;
    margin-right: 0.3325rem;
  }
}
/deep/.el-tabs__active-bar {
  height: 0.25rem;
}
/deep/.el-tabs__item.is-active {
  color: #333;
  border-bottom-color: #ff5e4e;
}
/deep/.el-tabs__item:hover {
  color: #333;
}
/deep/.el-tabs__active-bar {
  background-color: #ff5e4e;
}
/deep/.el-tabs__nav-wrap::after {
  background-color: #fff;
}
/deep/.el-tabs__nav {
  padding: 0.4375rem 0;
}
/deep/.el-tabs__item {
  font-size: 1rem;
}
/deep/.el-input__inner {
  border-radius: 1.25rem;
  border-color: #c0c4cc;
}
/deep/.el-col-8 {
  margin-top: 0.75rem;
}
main {
  height: 100%;
  width: 100%;
  background-color: #eff1f4 !important;
  padding-bottom: 4.625rem;
  box-sizing: border-box;
  .w1200 {
    width: 75rem;
    margin: auto;
    background-color: #eff1f4;
    .gap {
      height: 0.75rem;
    }
    .wfd {
      background-color: #fff;
      padding: 2.5rem 2.5rem 1.875rem;
      box-sizing: border-box;
      .h160 {
        height: 10rem;
        display: flex;
        padding-right: 6.875rem;
        box-sizing: border-box;
        .pdl15 {
          padding-left: 0.9375rem;
          box-sizing: border-box;
          display: flex;
          flex-direction: column;
          .title {
            font-size: 1.25rem;
            color: #333;
            font-weight: bold;
          }
          .des {
            font-size: 1rem;
            color: #999;
            line-height: 2rem;
          }
        }
      }
      .h60 {
        height: 3.75rem;
        line-height: 3.75rem;
        padding-left: 1.25rem;
        box-sizing: border-box;
        background-color: #f7f7f7;
        display: flex;
        position: relative;
        .exnum {
          font-size: 1rem;
          color: #666;
          width: 45rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .labels {
          display: flex;
          justify-content: flex-end;
          width: 21.25rem;
          span {
            height: 1.875rem;
            line-height: 1.875rem;
            padding: 0 0.375rem;
            box-sizing: border-box;
            background-color: #ff9b4f;
            border-radius: 0.1875rem;
            margin: 0.9375rem 0.46875rem;
            color: #fff;
            box-sizing: border-box;
          }
        }
      }

      .h121 {
        margin: 3.125rem 0 1.5625rem;
        width: 100%;
        background: #fffafa;
        border: 0.1875rem dashed #ff5e4e;
        border-bottom: 0.1875rem dashed #ff5e4e !important;
        box-sizing: border-box;
        padding: 1.25rem 1.75rem 1.6875rem;
        box-sizing: border-box;
        font-size: 1.125rem;
        color: #666;
        line-height: 2rem;
        padding-top: 2.625rem;
        div {
          display: flex;
          margin-bottom: 0.9375rem;
          .iconfont {
            font-size: 1.75rem;
            color: #666;
            cursor: pointer;
          }
          span {
            margin: auto;
          }
        }
        /deep/.el-progress__text i {
          display: none;
        }
        /deep/.el-progress-bar__outer {
          width: 54.5625rem;
        }
      }

      .answer-list {
        span {
          padding: 0.5rem 0.625rem;
          background-color: #ccc;
          color: #fff;
          margin-right: 1.25rem;
          font-size: 1.125rem;
          border-radius: 0.1875rem;
          cursor: pointer;
        }
      }
      .time {
        font-size: 1rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
        position: relative;
        padding-top: 2.5rem !important;
        span {
          margin-right: 1.875rem;
        }
        .iconfont {
          font-size: 1.5rem;
          color: #ccc;
        }
        .end {
          position: absolute;
          right: -1.5rem;
          text-align: right;
        }
      }
    }
    //  听力总结
    .sst {
      padding: 1.875rem 2.5rem;
      background-color: #fff;
      position: relative;
      .content {
        width: 100%;
        height: 17.0625rem;
        background-color: #f7f7f7;
        resize: none;
        border: none;
        outline: none;
        padding: 1.25rem;
        color: #999;
        font-size: 1.125rem;
      }
      span {
        position: absolute;
        right: 3.4375rem;
        bottom: 0.625rem;
      }
    }
    .tishi {
      height: 10.0625rem;
      background-color: #ff5e4e;
      margin-top: 0.625rem;
      text-align: center;
      color: #fff;
      .h80 {
        height: 5rem;
        line-height: 5rem;
        font-size: 1.5rem;
      }
      .howopen {
        width: 12.5rem;
        height: 3.125rem;
        line-height: 3.125rem;
        border: 0.0625rem solid #ffffff;
        border-radius: 0.1875rem;
        margin: auto;
        font-size: 1rem;
        cursor: pointer;
      }
    }
    .myexercise {
      margin-top: 0.625rem;
      background-color: #fff;
      padding: 0 2.5rem;
      box-sizing: border-box;
      padding-bottom: 1.25rem;
      .no-data {
        width: 16.875rem;
        height: 11.25rem;
        margin: auto;
        text-align: center;
        color: #999;
        margin-bottom: 2.5rem;
        .iconfont {
          font-size: 6.875rem;
        }
      }
      .bt {
        height: 6.25rem;
        line-height: 6.25rem;
        font-size: 2rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
      }
      ul {
        li {
          display: flex;
          padding-bottom: 1.5rem;
          margin-bottom: 1.2rem;
          border-bottom: 1px solid #ccc;
          .avatar {
            width: 3rem;
            height: 3rem;
            margin-right: 0.625rem;
            border-radius: 50%;
            img {
              width: 100%;
              height: 100%;
              border-radius: 50%;
            }
          }
          .content {
            flex: 1;
            font-size: 1.5rem;
            color: #333;
            display: flex;
            flex-direction: column;
            .time {
              font-size: 1rem;
              color: #999;
              margin-left: 1.875rem;
            }
            .answer-history {
              font-size: 1.125rem;
              color: #666;
              margin-top: 1rem;
            }
          }
        }
      }
    }
  }
}
.btns {
  height: 4.125rem;
  width: 25.875rem;
  margin: auto;
  margin-top: 1.25rem;
  line-height: 3.125rem;
  .el-button {
    height: 2.5rem;
    color: #ff5e4e;
    font-size: 1.125rem;
    text-align: center;
    width: 5rem;
    border-radius: 0.3125rem;
    border: 0.0625rem solid #ff5e4e;
    background-color: #fffafa;
    margin-right: 1.25rem;
  }
}
.h100 {
  padding: 1.5625rem 2.5rem;
  background-color: #fff;
  .el-button {
    height: 2.5rem;
    color: #ff5e4e;
    font-size: 1.125rem;
    text-align: center;
    width: 5rem;
    border-radius: 0.3125rem;
    border: 0.0625rem solid #ff5e4e;
    background-color: #fffafa;
    margin-right: 1.25rem;
  }
  .el-switch {
    margin-left: 1rem;
    margin-right: 0.5rem;
  }
  .el-input {
    width: 14.875rem;
    margin-left: 13rem;
    margin-right: 1.875rem;
    ::placeholder {
      font-size: 1.125rem;
      color: #cccccc;
    }
  }
  /deep/.el-input-group__append {
    padding: 0 1rem;
    background-color: #ff5e4e;
    color: #fff;
    border: 0.0625rem solid #ff5e4e;
    box-sizing: border-box;
    font-size: 1.125rem;
    cursor: pointer;
  }
  /deep/.el-input__inner {
    height: 3.125rem;
    width: 14.875rem;
    border-radius: 0.3125rem 0 0 0.3125rem;
    overflow: hidden;
    border-color: #ededed;
    border-right: none;
  }
  .prev,
  .next {
    width: 5rem;
    height: 2.5rem;
    background: #fffafa;
    border: 0.0625rem solid #ff5e4e;
    box-sizing: border-box;
    border-radius: 0.3125rem;
    color: #ff5e4e;
    margin-top: 0.8125rem;
    margin-left: 1rem;
    padding-left: 0.75rem;
    font-size: 1.125rem;
  }
  .answer-box {
    .title {
      font-size: 1.75rem;
      color: #333;
      height: 5.3125rem;
      line-height: 5.3125rem;
    }
    .content {
      font-size: 1.125rem;
      color: #666;
      line-height: 2rem;
    }
    .origin {
      font-size: 1.125rem;
      color: #666;
      line-height: 2rem;
      .origin-title {
        font-size: 1.5rem;
        color: #ccc;
        margin-top: 1.875rem;
      }
    }
  }
}
.icon-lingdang1 {
  color: #eee;
  line-height: 3.75rem;
}
.avatar {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  display: inline-block;
  vertical-align: middle;
}
.username {
  display: inline-block;
  margin-left: 0.625rem;
  font-weight: bold;
  font-size: 1rem;
  color: #333333;
  width: 6.25rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.jj-bg {
  background: #ff9b4f;
}
.jj-color {
  color: #ff9b4f;
}
.lxt-bg {
  background: #7cc3d6;
}
.lxt-color {
  color: #7cc3d6;
}
.wrong-bg {
  background: #ff5e4e;
}
.wrong-color {
  color: #ff5e4e;
}
.gpzt-bg {
  background: #13bfbf;
}
.gpzt-color {
  color: #13bfbf;
}
.lxcs-bg {
  background: #99bbe4;
}
.lxcs-color {
  color: #99bbe4;
}
.ylx-bg {
  background: #94d0f7;
}
.ylx-color {
  color: #94d0f7;
}

.timu {
  width: 100%;
  background: #fff;
  box-sizing: border-box;
  padding: 0 2.5rem;
  box-sizing: border-box;
  font-size: 1.125rem;
  color: #666;
  line-height: 2rem;
  padding-bottom: 20px;
  .time {
    margin-top: 2rem;
  }
}
</style>
